package com.android.webframework;

public class PArrayList<T> extends PLinearList<T>
{
	private Object[] array;
	private int capacity;
	
	/**
	 * Default constructor makes the capacity = 10
	 */
	public PArrayList()
	{
		capacity = 10;
		length = 0;
		array = new Object[capacity];
	}
	
	/**
	 * Argument constructor
	 * @param capacity sets the capacity
	 */
	public PArrayList(int capacity)
	{
		if(capacity < 1)
		{
			System.out.println("Capacity must be > 1");
			System.out.println("Capacity default set to 10");
			this.capacity = 10;
		}
		else
			this.capacity = capacity;
			
		length = 0;
		array = new Object[this.capacity];
	}
	
	/**
	 * Returns if the list is empty 
	 * @return check if the list is empty
	 * @see boolean
	 */
	public boolean empty()
	{
		return length == 0;
	}
	
	/**
	 * Returns the size of the list
	 * @return the number of elements in the list
	 * @see int
	 */
	public int size()
	{
		return length;
	}
	
	/**
	 * Returns the capacity of the list
	 * @return the number of elements in the list
	 * @see int
	 */
	public int capacity()
	{
		return capacity;
	}
	
	/**
	 * Clears the contents of the list
	 */
	public void clear()
	{
		length = 0;
		array = new Object[capacity];
	}
	
	/**
	 * Checks to see if the index is valid
	 * @param  index  location to insert in the list
	 * @return the index is valid or not
	 * @see boolean
	 */
	public boolean checkIndex(int index) throws IndexOutOfBoundsException
	{
		if(index < 0 || index >= capacity)
		{
			throw new IndexOutOfBoundsException();
		}
		return true;
	}
	
	/**
	 * Changes the capacity of the array
	 */
	public void changeLength()
	{
		Object[] temp = new Object[2*capacity];
		for(int i=0; i<length; i++)
			temp[i] = array[i];
		capacity *= 2;
		array = temp;
	}
	
	/**
	 * Appends an element to the end of the list
	 * @param element object to be added to the list
	 */
	public void add(Object element)
	{
		array[length] = element;
		length++;
		if(length == capacity)
			changeLength();
	}
	
	/**
	 * Removes the element from the list at the specified index
	 * @param  index the position in the list
	 */
	public void remove(int index)
	{
		if(checkIndex(index))
		{
			for(int i=index; i<length; i++)
				array[i] = array[i+1];
			length--;
		}
	}
	
	/**
	 * Gets a value from the list
	 * @param  index the position in the list
	 * @return the value in the list at the specified index
	 * @see Object
	 */
	public Object get(int index)
	{
		if(checkIndex(index))
			return array[index];
		else
			return null;
	}
	
	/**
	 * Returns the index of the element in the list
	 * @param  element the object of interest
	 * @return the index of the specified element
	 * @see int
	 */
	public int indexOf(Object element)
	{
		for(int i=0; i<length; i++)
			if(array[i] == element)
				return i;
		return -1;
	}
	
	/**
	 * Print the list
	 */
	public void print()
	{
		System.out.println(toString());
	}
	
	/**
	 * Returns a String representation of the list
	 * @return the list in String format
	 * @see String
	 */
	public String toString()
	{
		String s = "PArrayList Size: " + length + "\n[";
		for(int i=0; i<length; i++)
		{
			s += array[i];
			if(i != length-1)
				s += ", ";
		}
		s += "]";
		return s;
	}
}
